package com.canbot.u05.activity.dance

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.customvoice.tool.GridSpacingItemDecoration
import com.canbot.u05.activity.logd
import com.canbot.u05.activity.loge
import com.canbot.u05.activity.showToast
import com.canbot.u05.adapter.dance.DancePicAdapter
import com.canbot.u05.bean.dance.DancePicItemBean
import com.canbot.u05.bean.dance.DefaultPicBean
import com.canbot.u05.network.DanceUtil
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.DisplayUtils
import com.canbot.u05.utils.Logger
import com.canbot.u05.utils.PathConst
import com.canbot.u05.widget.LoadingDialogUtils
import kotlinx.android.synthetic.main.activity_dance_choose_pic.*

class DanceChoosePicActivity : BaseActivityNoTitle() {
        private val mbrowList: MutableList<DancePicItemBean> = mutableListOf()
        private lateinit var context: Context
        private val constant = 8
        private var homePage: Int = 0
        private var index: Int = 0
        private var selectedImage = ""
        private var actionAdapter:DancePicAdapter? = null
        private val tipSound = listOf("wdfm_wdgj_u05_001.mp3","wdfm_wdgj_u05_002.mp3")
        private var dialogLoading: Dialog? = null
        override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_dance_choose_pic)
                context = this
                initView()
                initData()
                dance_viewpager_industry.adapter = pagerAdapter
                dance_viewpager_industry.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                        override fun onPageScrolled(i: Int, v: Float, i1: Int) {}

                        override fun onPageSelected(i: Int) {
                                homePage = i
                                isPress(i)

                        }

                        override fun onPageScrollStateChanged(i: Int) {

                        }
                })
                dance_left.isSelected = true
                dance_right.isSelected = true
        }
        fun isPress(homePage: Int) {
                when (homePage) {
                        0 -> {
                                dance_left.isSelected = true
                                dance_right.isSelected = false
                        }
                        (mbrowList.size + (constant-1)) / constant - 1 -> {
                                dance_right.isSelected = true
                                dance_left.isSelected = false
                        }
                        else -> {
                                dance_left.isSelected = false
                                dance_right.isSelected = false
                        }
                }
        }
        /**
         * 初始化数据
         */
        fun initData(){
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this@DanceChoosePicActivity, resources.getString(R.string.request))
                dialogLoading?.show()
                DanceUtil.defaultPic(object :DanceUtil.DanceCallBack<DefaultPicBean>{
                        override fun onSuccess(result: DefaultPicBean) {
                                dialogLoading?.dismiss()
                                result.let {
                                        if (it.error_code == 0) {
                                                it.content.forEach {
                                                        val dancePicItemBean = DancePicItemBean(false,it) {index,image->
                                                                logd("选择图片"+image)
                                                                selectedImage = image
                                                                mbrowList.forEach{
                                                                        it.isChecked = false
                                                                }
                                                                val dancePicItemBean = mbrowList.get(homePage*constant+index)
                                                                dancePicItemBean.isChecked = true
                                                                mbrowList[homePage*constant+index] = dancePicItemBean
                                                                pagerAdapter.notifyDataSetChanged()
                                                        }
                                                        mbrowList.add(dancePicItemBean)
                                                }
                                        }
                                        val pageSize = (mbrowList.size + (constant-1)) / constant
                                        if (pageSize>1){
                                                dance_left.isSelected = true
                                                dance_right.isSelected = false
                                        }else{
                                                dance_left.isSelected = true
                                                dance_right.isSelected = true
                                        }
                                        val random = (0..1).random()
                                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.PAD_DANCE_TIP_PATH + tipSound[random])
                                        pagerAdapter.notifyDataSetChanged()
                                }

                        }

                        override fun onFailure() {
                                dialogLoading?.dismiss()
                        }

                })
        }
        private fun initView() {

                btn_back.setOnClickListener {
                        finish()
                        overridePendingTransition(R.anim.face_zoom_enter,
                                R.anim.face_zoom_exit)
                }
                btn_sure.setOnClickListener {
                        if (TextUtils.isEmpty(selectedImage)){
                                showToast("请选择图片")
                                return@setOnClickListener
                        }
                        val intent = intent.putExtra("image",selectedImage)
                        setResult(Activity.RESULT_OK,intent)
                        finish()
                        overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit)
                }
                dance_left.setOnClickListener {
                        Logger.e(DanceManagerActivity.TAG, "左边按钮")
                        if (dance_viewpager_industry.currentItem == 0) {
                                Logger.e(DanceManagerActivity.TAG, "mViewPager.getCurrentItem() = " + dance_viewpager_industry.currentItem)
                                return@setOnClickListener
                        } else {
                                dance_viewpager_industry.setCurrentItem(dance_viewpager_industry.currentItem - 1, true)
                        }
                }
                dance_right.setOnClickListener {
                        Logger.e(DanceManagerActivity.TAG, "右边按钮")
                        if (dance_viewpager_industry.currentItem == pagerAdapter.count - 1) {
                                Logger.e(DanceManagerActivity.TAG, "mViewPager.getCurrentItem() = " + dance_viewpager_industry.currentItem)
                                return@setOnClickListener
                        } else {
                                dance_viewpager_industry.setCurrentItem(dance_viewpager_industry.currentItem + 1, true)
                        }
                }
        }

        override fun onPause() {
                super.onPause()
                dialogLoading?.dismiss()
        }
        private val pagerAdapter = object : PagerAdapter() {

                override fun isViewFromObject(arg0: View, arg1: Any): Boolean {

                        return arg0 === arg1
                }

                override fun getCount(): Int {

                        return (mbrowList.size + (constant-1)) / constant
                }

                override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
                        container.removeView(`object` as View)
                }

                override fun instantiateItem(container: ViewGroup, position: Int): Any {
                        val inflater = LayoutInflater.from(container.context)
                        val view = inflater.inflate(R.layout.layout_dance_recyclerview, container, false)

                        val mbrowList1 = mbrowList.subList(position * constant, if (position * constant + constant > mbrowList.size) mbrowList.size else position * constant + constant)
                        val recyclerView = view.findViewById<RecyclerView>(R.id.gv_content_action)
                        recyclerView.layoutManager = GridLayoutManager(context, 4)  //网格布局，每行为4
                        recyclerView.addItemDecoration(GridSpacingItemDecoration(4, DisplayUtils.dip2px(context, 104f), false))
                        actionAdapter = DancePicAdapter(context, mbrowList1)
                        recyclerView.adapter = actionAdapter
                        actionAdapter?.setIndex(index)
                        container.addView(view)

                        return view
                }

                override fun getItemPosition(`object`: Any): Int {
                        return PagerAdapter.POSITION_NONE
                }
        }
}
